﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DienLib.ReportLib
{
    public class Command
    {

        static string[] _UserDateTimeFormat = { "dd MMM yyyy", "d MMM yyyy", "dd MMM yy", "d MMM yy" ,
                                              "MMM dd yyyy", "MMM d yyyy", "MMM dd yy", "MMM d yy"};
        /// <summary>
        /// Get Date from any string that contain Date in any format
        /// </summary>
        /// <param name="textDate">String that contain Date in any format</param>
        /// <returns>Date in that String in DateTime format</returns>
        public static DateTime ParseDate(string textDate)
        {
            if (string.IsNullOrEmpty(textDate)) throw new InvalidOperationException("textDate is Null, can not ParseDate from TextDate");

            string txt = string.Empty;

            // Parse below assume Month in the middle of Day & Year so Number appear first & last in the string
            int _startIndexNumeric = textDate.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            int _stopIndexNumeric = textDate.LastIndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            foreach (string s in new string[] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" })
            {   // Simulate IndexOfAny StringArray
                int _indexMonth = textDate.ToLower().IndexOf(s);
                if (_indexMonth >= 0)
                {   // Tim thay Month trong chuoi
                    if (_indexMonth < _startIndexNumeric) // Month xuat hien truoc
                        txt = textDate.Substring(_indexMonth, _stopIndexNumeric - _indexMonth + 1);
                    else // Month nam giua ngay va nam
                        txt = textDate.Substring(_startIndexNumeric, _stopIndexNumeric - _startIndexNumeric + 1);
                }
            }

            if (txt == string.Empty) throw new InvalidOperationException("This string does not contain Day Month Year !");

            //Then parse to get Day Month Year
            string[] _patterns = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns();
            List<string> _allPatterns = new List<string>(_patterns);
            foreach (string s in _patterns)
            {
                if (s.Contains('-')) { _allPatterns.Add(s.Replace('-', ' ')); } // DateTimePattern with '-' will be replace by space also
            }
            //Add User DateTime Format
            _allPatterns.AddRange(_UserDateTimeFormat);

            try { return DateTime.ParseExact(txt, _allPatterns.ToArray(), System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces); }
            catch (Exception ex)
            {   // If error then parse by manual
                throw ex;
            }
        }

        public EnumsStructs.ServiceType GetServiceType(string fromString)
        {
            if (fromString == string.Empty) throw new InvalidOperationException("fromString is Empty ! Can not convert to ServiceType");
            string txt = fromString.ToLower();

            EnumsStructs.ServiceType type = new EnumsStructs.ServiceType(txt);
            return type;
        }
    }
}